package com.jtjrenren.android.taxi.passenger.core.pay.result;

import android.app.Activity;
import android.content.Intent;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.manger.AppManger;
import com.jtjrenren.android.taxi.passenger.manger.push.cmd.ControlCmd;
import com.jtjrenren.android.taxi.passenger.base.BaseActivity;
import com.jtjrenren.android.taxi.passenger.utils.ModuleUtils;
import com.library.utils.StringUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/3/28.
 */
public class PayResultActivity extends BaseActivity {
    public static final int WHAT_CLOSE = 100;

    @BindView(R.id.pay_result_success)
     LinearLayout ll_success;
    @BindView(R.id.pay_result_error)
     TextView tv_failed;
    @BindView(R.id.title)
    TextView tv_title;

    private String orderSerno; //电召单号
    private boolean isPaySuccess = false; //是否支付成功
    private String money = "";

    public static Intent getDiyIntent(Activity context,String money,String orderID,boolean result){
        Intent intent=new Intent(context,PayResultActivity.class);
        intent.putExtra("money",money);
        intent.putExtra("orderID",orderID);
        intent.putExtra("result",result);
        return intent;
    }


    @Override
    public int getLayoutId() {
        return R.layout.aty_pay_result;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeMessages(WHAT_CLOSE);
    }

    @Override
    public void init() {
        initHandler();

        tv_title.setText(getString(R.string.zhifujieguo));

        orderSerno = getIntent().getStringExtra("orderID");
        isPaySuccess = getIntent().getBooleanExtra("result", false);

        if (getIntent().hasExtra("money")) {
            money = getIntent().getStringExtra("money");
        }

        if (isPaySuccess) {
            ll_success.setVisibility(View.VISIBLE);
            tv_failed.setVisibility(View.GONE);
        } else {
            tv_failed.setVisibility(View.VISIBLE);
            ll_success.setVisibility(View.GONE);
        }

        sendPayResulttoControl(isPaySuccess);

        handler.sendEmptyMessageDelayed(WHAT_CLOSE, 2000);
    }

    @Override
    protected void handMsg(Message msg) {
        super.handMsg(msg);
        switch (msg.what) {
            case WHAT_CLOSE:
                Intent intent = new Intent();
                intent.putExtra("result", isPaySuccess);
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }

    @OnClick(R.id.title_left)
    public void clickBack(){
        Intent intent = new Intent();
        intent.putExtra("result", isPaySuccess);
        setResult(RESULT_OK, intent);
        finish();
    }

    /**
     * 向调度中心发送支付结果
     *
     * @param isSuccess
     */
    private void sendPayResulttoControl(boolean isSuccess) {
        if (!StringUtils.isEmpty(money))
            money = ModuleUtils.DesEncry(money);
        String uploadPayResultCmd = ControlCmd.getUploadPayResultCmd(orderSerno, money, isSuccess);
        AppManger.getInstance().sendMsgToControl(uploadPayResultCmd);
    }
}
